package com.example;

import java.util.concurrent.Semaphore;

/**
 * @Author: wyp
 * @Date: 2021/9/9 23:09
 */
public class SemaphoreDemo {
    public static void main(String[] args) {
        //定义餐馆有10桌
        Semaphore semaphore = new Semaphore(10);

        //100桌客人待上桌
        for (int i = 0; i < 100; i++) {
            new Thread(()->{
                try {
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName()+"\t 上桌了~");
                    //吃300毫秒
                    Thread.sleep(300);
                    System.out.println(Thread.currentThread().getName()+"\t 客人吃完了，欢迎再次光临~");
                    semaphore.release();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            },String.valueOf(i)).start();
        }
    }
}
